package com.woniuxy01;

import java.util.Arrays;
import java.util.Comparator;

/**
 * @author 作者
 * @create 2025-06-03 16:37
 */
public class Test01 {
    public static void main(String[] args) {
        test02();
    }

    private static void test02() {
        Student[] students = new Student[4];
        students[0] = new Student("zs",11);
        students[1] = new Student("ls",15);
        students[2] = new Student("aa",12);
        students[3] = new Student("ww",13);
        System.out.println("排序前: " + Arrays.toString(students));
        //sort(数组)，默认使用Comparble接口的compareTo()方法，比较大小
        //自然排序
        Arrays.sort(students);
        System.out.println("默认自然排序，年龄升序：: " + Arrays.toString(students));
        //Compartor:比较器，自定义排序
        Arrays.sort(students,new StudentAgeDesc());
        System.out.println("比较器，年龄降序：: " + Arrays.toString(students));
        Arrays.sort(students,new StudentNameAsc());
        System.out.println("比较器，姓名升序：: " + Arrays.toString(students));

        Arrays.sort(students, new Comparator() {
            @Override
            public int compare(Object o1, Object o2) {
                if (o1 instanceof Student && o2 instanceof Student) {
                    Student one = (Student) o1;
                    Student two = (Student) o2;
                    return one.getName().compareTo(two.getName()) * -1;
                }
                return 0;
            }
        });
        System.out.println("比较器，姓名降序：: " + Arrays.toString(students));

    }

    private static void test01() {
        int[] a = {1, 7, 2, 9, 3, 6, 4, 5};
        Arrays.sort(a);
        System.out.println(Arrays.toString(a));
        String[] strs = {"aaa", "aa", "bbb", "abc", "bcd"};
        Arrays.sort(strs);
        System.out.println(Arrays.toString(strs));
    }
}
